// pages/video/video.js
const app = getApp();
const ipUrl = app.globalData.ipUrl + "/";
import {
  getVediosType,
  getVedios,
  getRandomVideos
} from "../../apis/getVideos"
Page({
  changeFollowed(event) {
    console.log(event.target.id)
    this.data.video[event.target.id].followed = !this.data.video[event.target.id].followed;
    this.setData({
      video: this.data.video
    })
  },
  changeAgreed(event) {
    console.log(event.target.id)
    this.data.video[event.target.id].agreed = !this.data.video[event.target.id].agreed;
    if(/^\d+$/.test(this.data.video[event.target.id].agreeNumber)){
    this.data.video[event.target.id].agreeNumber = this.data.video[event.target.id].agreeNumber + 1;
    }
    this.setData({
      video: this.data.video
    })
  },
  changeAgreed2(event) {
    console.log(event.target.id)
    this.data.video[event.target.id].agreed = !this.data.video[event.target.id].agreed;
    if(/^\d+$/.test(this.data.video[event.target.id].agreeNumber)){
    this.data.video[event.target.id].agreeNumber = this.data.video[event.target.id].agreeNumber - 1;
    }
    this.setData({
      video: this.data.video
    })
  },
  changeCollected(event) {
    console.log(event.target.id)
    this.data.video[event.target.id].collected = !this.data.video[event.target.id].collected;
    this.setData({
      video: this.data.video
    })
  },
  onTabsChange(event) {
    console.log(event.detail.label);
    this.setData({
      channel: `${event.detail.label}`
    })
    if(this.data.channel == "推荐"){
      
      getRandomVideos().then((res) => {
        console.log(res);
        this.setData({
          video: res.vediolist
  
        })
        let i = 0;
        for(i=0;i<this.data.video.length;i++)
        {
          if(this.data.video[i].watched>=10000)
          {
            this.data.video[i].watched = this.data.video[i].watched/10000+"万";
          }
          if(this.data.video[i].agreeNumber>=10000)
          {
            this.data.video[i].agreeNumber = this.data.video[i].agreeNumber/10000+"万";
          }
          let DataArray = this.data.video
          this.setData({
            video:DataArray
          })
        }
        console.log(this.data.video);
      })












    }
  },

  onTabsClick(event) {
    this.setData({
      channel: `${event.detail.label}`,
      nameid: this.data.selectChannel[event.detail.value].nameid
    })
    if(this.data.channel !== "推荐"){
      getVedios(this.data.selectChannel[event.detail.value].nameid).then((res) => {
        console.log(res);
        this.setData({
          video: res.vediolist
  
        })
        let i = 0;
        for(i=0;i<this.data.video.length;i++)
        {
          if(this.data.video[i].watched>=10000)
          {
            this.data.video[i].watched = this.data.video[i].watched/10000+"万";
          }
          if(this.data.video[i].agreeNumber>=10000)
          {
            this.data.video[i].agreeNumber = this.data.video[i].agreeNumber/10000+"万";
          }
          let DataArray = this.data.video
          this.setData({
            video:DataArray
          })
        }
        console.log(this.data.video);
      })
    }

  },
  onClick() {
    console.log('click grid-item');
  },
  clickCell: function (event) {
    console.log(event)
    const newsurl = event.currentTarget.id;
    const nameid1 = this.data.nameid
    const nameid2 = event.currentTarget.dataset.value
    const Array = event.currentTarget.dataset.value2
    console.log(nameid1)
    wx.navigateTo({
      url: `/pages/webViewVideo/webViewVideo?url=${encodeURIComponent(newsurl)}&nameid=${encodeURIComponent(nameid1)}&nameid2=${encodeURIComponent(nameid2)}&Array=${encodeURIComponent(JSON.stringify(Array))}`
    });
  },

  /**
   * 页面的初始数据
   */
  data: {
    datasave: "",
    nameid: "",
    ipUrl: "",
    selectChannel: [],
    channel: "",
    video: [

    ],

  },
  handlePopup(e) {
    const {
      item
    } = e.currentTarget.dataset;

    this.setData({
        cur: item,
      },
      () => {
        this.setData({
          visible: true
        });
      },
    );
  },
  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options, event) {
    this.setData({
      channel: 0,
      ipUrl: ipUrl,

    })
    console.log('推荐');
    getVediosType().then((res) => {
      
      // 更新 typeArray
      console.log(res.vediotype)
      const selectChannel = this.data.selectChannel.concat(res.vediotype);
      // 排序
      selectChannel.sort((a, b) => a.colid - b.colid);
      // 设置排序后的数组
      this.setData({
        selectChannel: selectChannel,
        nameid: selectChannel[0].nameid
      });
      getRandomVideos().then((res) => {
        
        console.log(res);
        this.setData({
          video: res.vediolist

        })
        let i = 0;
        for(i=0;i<this.data.video.length;i++)
        {
          if(this.data.video[i].watched>=10000)
          {
            this.data.video[i].watched = this.data.video[i].watched/10000+"万";
          }
          if(this.data.video[i].agreeNumber>=10000)
          {
            this.data.video[i].agreeNumber = this.data.video[i].agreeNumber/10000+"万";
          }
          let DataArray = this.data.video
          this.setData({
            video:DataArray
          })
        console.log(this.data.video)
        }
        console.log(this.data.video);

      })

    });
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})